package com.cofluent.web.server;

import java.util.Map;

import org.w3c.dom.Element;

import com.cofluent.web.client.model.Build;

public class BuildElement extends XMLSerializerUtil
{
	private static final String CVS_TAG_FIELD = "cvs-tag";
	private static final String RELEASE_NUMBER_FIELD = "release-number";
	private static final String BUILD_NUMBER_FIELD = "build-number";
	private static final String VERSION_NUMBER_FIELD = "version-number";
	private static final String DATA_NUMBER_FIELD="data-number";
	private static final String SYSTEMC_SIMULATION_LIBRARY_FIELD="systemc-simulation-library";
	private static final String META_GENERATOR_FIELD="meta-generator";
	private static final String USER_MANUAL_FIELD="user-manual";
	private static final String GNU_COMPILER_FIELD="gnu-compiler";
	private static final String JDK_FIELD="jdk";

	private Build build=new Build();
	
	public void setDataNumber(String dataNumber)
	{
		setField(DATA_NUMBER_FIELD, dataNumber);
		build.setDataNumber(dataNumber);
	}

	public void setGnuCompiler(String gnuCompiler)
	{
		setField(GNU_COMPILER_FIELD, gnuCompiler);
		build.setGnuCompiler(gnuCompiler);
	}

	public void setJdk(String jdk)
	{
		setField(JDK_FIELD, jdk);
		build.setJdk(jdk);
	}

	public void setMetaGenerator(String metaGenerator)
	{
		setField(META_GENERATOR_FIELD, metaGenerator);
		build.setMetaGenerator(metaGenerator);
	}

	public void setSystemCSimulationLibrary(String systemCSimulationLibrary)
	{
		setField(SYSTEMC_SIMULATION_LIBRARY_FIELD, systemCSimulationLibrary);
		build.setSystemCSimulationLibrary(systemCSimulationLibrary);
	}

	public void setUserManual(String userManual)
	{
		setField(USER_MANUAL_FIELD, userManual);
		build.setUserManual(userManual);
	}

	public String getDataNumber()
	{
		return build.getDataNumber();
	}

	public String getGnuCompiler()
	{
		return build.getGnuCompiler();
	}

	public String getJdk()
	{
		return build.getJdk();
	}

	public String getMetaGenerator()
	{
		return build.getMetaGenerator();
	}

	public String getSystemCSimulationLibrary()
	{
		return build.getSystemCSimulationLibrary();
	}

	public String getUserManual()
	{
		return build.getUserManual();
	}
	
	private String getParameter(Map map, String parameterName)
	{
		Object parameter=map.get(parameterName);
		if(parameter==null)
			throw new RuntimeException("Parameter " + parameterName + " must be defined");
		else if (parameter instanceof String)
			return (String)parameter;
		else if (parameter instanceof String[])
		{
			String[] params=(String[])parameter;
			if(params.length==1)
				return params[0];
			else
				throw new RuntimeException("Parameter " + parameterName + " has " + params.length + " values instead of 1.");				
		}
		else
			throw new RuntimeException("Wrong type for parameter " + parameterName + ": " + parameter.getClass().getName());
	}

	public void setValues(Map map)
	{
		setReleaseNumber(getParameter(map,RELEASE_NUMBER_FIELD));
		setBuildNumber(getParameter(map,BUILD_NUMBER_FIELD));
		setCvsTag(getParameter(map,CVS_TAG_FIELD));
		setVersionNumber(getParameter(map,VERSION_NUMBER_FIELD));
		setDataNumber(getParameter(map,DATA_NUMBER_FIELD));
		setSystemCSimulationLibrary(getParameter(map,SYSTEMC_SIMULATION_LIBRARY_FIELD));
		setMetaGenerator(getParameter(map,META_GENERATOR_FIELD));
		setUserManual(getParameter(map,USER_MANUAL_FIELD));
		setGnuCompiler(getParameter(map,GNU_COMPILER_FIELD));
		setJdk(getParameter(map,JDK_FIELD));
	}

	public String getBuildNumber()
	{
		return build.getBuildNumber();
	}

	public String getCvsTag()
	{
		return build.getCvsTag();
	}

	public String getReleaseNumber()
	{
		return build.getReleaseNumber();
	}

	public String getVersionNumber()
	{
		return build.getVersionNumber();
	}

	public void setCvsTag(String cvsTag)
	{
		setField(CVS_TAG_FIELD, cvsTag);
		build.setCvsTag(cvsTag);
	}

	public void setBuildNumber(String buildNumber)
	{
		setField(BUILD_NUMBER_FIELD, buildNumber);
		build.setBuildNumber(buildNumber);
	}

	public void setReleaseNumber(String releaseNumber)
	{
		setField(RELEASE_NUMBER_FIELD, releaseNumber);
		build.setReleaseNumber(releaseNumber);
	}

	public void setVersionNumber(String versionNumber)
	{
		setField(VERSION_NUMBER_FIELD, versionNumber);
		build.setVersionNumber(versionNumber);
	}

	public BuildElement(Element element)
	{
		super(element);
		build.setCvsTag(getField(CVS_TAG_FIELD));
		build.setReleaseNumber(getField(RELEASE_NUMBER_FIELD));
		build.setBuildNumber(getField(BUILD_NUMBER_FIELD));
		build.setVersionNumber(getField(VERSION_NUMBER_FIELD));
		build.setDataNumber(getField(DATA_NUMBER_FIELD));
		build.setSystemCSimulationLibrary(getField(SYSTEMC_SIMULATION_LIBRARY_FIELD));
		build.setMetaGenerator(getField(META_GENERATOR_FIELD));
		build.setUserManual(getField(USER_MANUAL_FIELD));
		build.setGnuCompiler(getField(GNU_COMPILER_FIELD));
		build.setJdk(getField(JDK_FIELD));
	}

	public Build getBuild() {
		return build;
	}
}
